console.log('加载choosePrice.js');
myApp.onPageInit('treasure_hunter_chooseprice', function (page) {
	  var actId = $$('#treasure_hunter_chooseprice').data('actId');
	  var issueNum = $$('#treasure_hunter_chooseprice').data('issueNum');
	  var actName = $$('#treasure_hunter_chooseprice').data('actName');
	  var priceSingle = $$('#treasure_hunter_chooseprice').data('price');
	  var priceGroup =$$('#treasure_hunter_chooseprice').data('priceGroup');
	  
	  //更改报名方式
	  $$('input[name="pricetype"]').change(function(){
		  //隐藏所有信息填写
		  $$('#treasure_hunter_chooseprice .register_wrapper').hide();
		  $$('#treasure_hunter_chooseprice .register_wrapper').removeClass('active');
		  //获取报名方式
		  var type=$$(this).val();
		  if(type=='single'){
			  //个人报名
			  $$('#treasure_hunter_chooseprice .single_wrapper').show();
			  $$('#treasure_hunter_chooseprice .single_wrapper').addClass('active');
		  }else{
			  //团体报名
			  $$('#treasure_hunter_chooseprice .group_member_wrapper').show();
			  $$('#treasure_hunter_chooseprice .group_member_wrapper').addClass('active');
			  //弹出组团报名须知
			  myApp.modal({
				  title: '团体报名须知',
            	  text: "<p>1.如果您受别人邀请加入团队，请在表格中输入该团队的组团口令。</p><p>2.如果您想要组建自己的团队，请点击表格下方《我要邀请他人》。则系统会生成您的组团口令，您只需将口令发给您想要邀请的人，让其用您的组团口令报名即可。</p>",
            	  buttons:[
            	  	{
            	  		text:'朕已知晓',
            	  		bold: true
            	  	}
            	  ],
			  });
		  }
	  });
	  
	  //我要邀请他人
	  $$('#treasure_hunter_chooseprice .become_host').click(function(){
		  //隐藏所有信息填写
		  $$('#treasure_hunter_chooseprice .register_wrapper').hide();
		  $$('#treasure_hunter_chooseprice .register_wrapper').removeClass('active');
		  $$('#treasure_hunter_chooseprice .group_host_wrapper').show();
		  $$('#treasure_hunter_chooseprice .group_host_wrapper').addClass('active');
		  //生成6位组团口令
		  var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
		  var charslen = chars.length;
          var str = "";
          var len = 6;
          
          for ( var i = 0; i < len; i++ )  {
        	  var num = Math.random();//得到一个0到1之间的随机数
        	  num = Math.floor(num * charslen);
			  str += chars.substr(num, 1);  
		  }
          $$('.group_host_wrapper .groupcode').html(str);
		  
	  });
	  //接受别人组团邀请，判断组团口令是否存在
	  $$('input[name="inviNum"]').change(function(){
		  var inviNum = $$(this).val();
		  //console.log(actId,issueNum,inviNum);
		  
		  var res = checkGrpcodeisExist(actId,issueNum,inviNum);
		  //console.log(res);
		  if(res){
			  //该组人数没有报满
			  if(res.group.length<4){
				  //滚动
				  if(res.group.length<2){
					 //已有1人报名
					 //生成团队成员信息
    				  var html_treasure_hunter_group_one_member = myApp.treasureHunterGroupOneMemberTemplate(res.group);
    				  $$('#treasure_hunter_chooseprice .group-members').html(html_treasure_hunter_group_one_member);
    				  
					  var treasurehuntergroupmembersswiper = new Swiper('.swiper-treasure-hunter-group-members', {
              	  		pagination:".swiper-pagination-treasure-hunter-group-members",
                    });
				  }else{
					  //已有1人以上报名报名
					  //生成团队成员信息
    				  var html_treasure_hunter_group_members = myApp.treasureHunterGroupMembersTemplate(res.group);
    				  $$('#treasure_hunter_chooseprice .group-members').html(html_treasure_hunter_group_members);
    				  
					  var treasurehuntergroupmembersswiper = new Swiper('.swiper-treasure-hunter-group-members', {
              	  		pagination:".swiper-pagination-treasure-hunter-group-members",
              	  		spaceBetween:20,
              	  	    slidesPerView:2,
              	  		loop:true,
              	  	    autoplay:5000,
              	  	    autoplayDisableOnInteraction:false
                    });
				  }
				  
			  }
			  //该组满员，但该用户之前没有付款成功
			  else if(res.userStatus=='UNPAID'){
				  myApp.alert('您已报名，请直接点击下方《提交》完成付款');
				  //生成团队成员信息
				  var html_treasure_hunter_group_members = myApp.treasureHunterGroupMembersTemplate(res.group);
				  $$('#treasure_hunter_chooseprice .group-members').html(html_treasure_hunter_group_members);
				  
				  var treasurehuntergroupmembersswiper = new Swiper('.swiper-treasure-hunter-group-members', {
          	  		pagination:".swiper-pagination-treasure-hunter-group-members",
          	  		spaceBetween:20,
          	  	    slidesPerView:2,
          	  		loop:true,
          	  	    autoplay:5000,
          	  	    autoplayDisableOnInteraction:false
                  });
			  }
			  //该组满员，且该用户已经付款
			  else if(res.userStatus=='PAID'){
				  myApp.alert('您已成功报名并完成付款，一个微信账号只能报名一次，无需重复报名');
				  //生成团队成员信息
				  var html_treasure_hunter_group_members = myApp.treasureHunterGroupMembersTemplate(res.group);
				  $$('#treasure_hunter_chooseprice .group-members').html(html_treasure_hunter_group_members);
				  
				  var treasurehuntergroupmembersswiper = new Swiper('.swiper-treasure-hunter-group-members', {
          	  		pagination:".swiper-pagination-treasure-hunter-group-members",
          	  		spaceBetween:20,
          	  	    slidesPerView:2,
          	  		loop:true,
          	  	    autoplay:5000,
          	  	    autoplayDisableOnInteraction:false
                  });
			  }
			  //该组满员，且该用户不属于该组
			  else{
				  myApp.alert('该团队已经满员，无法加入');
				  //清空组队口令
				  $$(this).val('');
			  }
			  
			  
		  }else{
			  //组团口令不存在，清空,alert提醒在checkGrpcodeisExist()函数中
			  $$(this).val('');
		  }
	  });
	  
	  //支付
	  $$("#treasure_hunter_chooseprice .pay").click(function(){
		  //console.log(actId,issueNum,actName);
		  
		  //判断用户是否已经报名该活动
		  var res = checkUserisRegistered(actId,issueNum);
		  //console.log(res);
		  
		  //用户没有报名
		  if(res.status=='UNREGISTERED'){
			  var type = "single";//报名方式
			  var inviNum = '';//组团口令
			  
			  if($$('input[name="pricetype"]:checked').val()=='group'){
				  var price = parseFloat(priceGroup);
				  price *= 100;
				  price = parseInt(price);
			  }else{
				  var price = parseFloat(priceSingle);
				  price *= 100;
				  price = parseInt(price);
			  }
			  //console.log(price,$$('input[name="pricetype"]:checked').val());return;
			  
			  //姓名
			  var name = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('input[name="name"]').val();
			  //性别
			  var gender = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('select[name="gender"]').val();
			  //微信号
			  var wxnum = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('input[name="wxnum"]').val();
			  //身份证号码
			  var IdcardNum = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('input[name="IdcardNum"]').val();
			  //电话号码
			  var telNum = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('input[name="telNum"]').val();
			  
			  //接受别人的组团邀请
			  if($$('#treasure_hunter_chooseprice .register_wrapper.active').hasClass('group_member_wrapper')){
				  var inviNum = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('input[name="inviNum"]').val();
				  if(!inviNum){
					  myApp.alert('请填写组团口令');
					  return;
				  }
				  type = "member";
			  }
			  
			  //自己组团
			  if($$('#treasure_hunter_chooseprice .register_wrapper.active').hasClass('group_host_wrapper')){
				  var inviNum = $$('#treasure_hunter_chooseprice .register_wrapper.active').find('.groupcode').html();
				  if(!inviNum){
					  myApp.alert('系统出错了');
					  return;
				  }
				  type = "host";
			  }
			  //console.log(name,gender,wxnum,IdcardNum,telNum,inviNum,type);
			  if(name && gender && wxnum && IdcardNum && telNum){
			      var re = userRegister(name,gender,wxnum,IdcardNum,telNum,inviNum,type,actId,actName,issueNum,price);
			      if(re){
			    	  //myApp.alert("报名成功！哇吖将对您的资料进行一些必要的审核，审核通过后才会进入相关支付流程。如有问题，请在公众号内点击《哇吖客服》留言。");
			    	  //注册成功跳转支付页面
			    	  location.href="http://api.wayatrip.com/treasurehunter/pay?actId="+actId+"&issueNum="+issueNum; 
			      }
			  }else{
				  myApp.alert('请补全信息');
				  return;
			  }
			  
		  }
		  //用户报名了，但是没有支付
		  else if(res.status=='UNPAID'){
			  //报名方式
			  switch(res.type){
				  case "single":
					  var unpaidnotice = "您已经以个人方式报名,但您没能付款成功。</p><p>如要完成付款，请点击继续付款。如要更改报名方式，请联系客服</p>";
					  break;
				  case "member":
					  var unpaidnotice = "<p>您已经接受别人的邀请加入了团队，组团口令为："+res.inviNum+"，但您没能付款成功。</p><p>如要完成付款，请点击继续付款。如要更改报名方式，请联系客服</p>";
					  break;
				  case "host":
					  var unpaidnotice = "<p>您已组建了自己的团队，组团口令为："+res.inviNum+",但您没能付款成功。</p><p>如要完成付款，请点击继续付款。如要更改报名方式，请联系客服</p>";
					  break;
				  default:
					  break;
			  }
			  //相应提醒
			  myApp.modal({
				  title: "哇吖旅行",
				  text : unpaidnotice,
				  buttons : [
					  {
					    text: '继续付款',
					    onClick: function() {
					    	//直接跳转支付页面
							location.href="http://api.wayatrip.com/treasurehunter/pay?actId="+actId+"&issueNum="+issueNum;
					    }
					  },
					  {
					    text: '取消',
					    onClick: function() {
					    	myApp.closeModal();
					    }
					  },              
				  ]
			  });
		  }
		  //用户报名且已经支付
		  else{
			  myApp.alert("您已成功报名并完成付款，一个微信账号只能报名一次，无需重复报名");
		  }
	  });
	  
	  //关闭loader
	  myApp.hideIndicator();
});